package com.ruoyi.web.controller.system.city;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.web.core.config.OssConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.city.CityModule;
import com.ruoyi.system.service.city.ICityModuleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 模块Controller
 * 
 * @author cjq
 * @date 2023-07-24
 */
@RestController
@RequestMapping("/system/city/module")
public class CityModuleController extends BaseController
{
    @Autowired
    private ICityModuleService cityModuleService;


    /**
     * 查询模块列表
     */
    @GetMapping("/moduleList")
    public List<CityModule> moduleList(CityModule cityModule)
    {

        List<CityModule> list = cityModuleService.selectCityModuleList(cityModule);
        return list;
    }

    /**
     * 查询模块列表
     */
    @PreAuthorize("@ss.hasPermi('city:module:list')")
    @GetMapping("/list")
    public TableDataInfo list(CityModule cityModule)
    {
        startPage();
        List<CityModule> list = cityModuleService.selectCityModuleList(cityModule);
        return getDataTable(list);
    }

    /**
     * 导出模块列表
     */
    @PreAuthorize("@ss.hasPermi('city:module:export')")
    @Log(title = "模块", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CityModule cityModule)
    {

        List<CityModule> list = cityModuleService.selectCityModuleList(cityModule);
        List<CityModule> imageList=new ArrayList<>();
        if (!list.isEmpty()){
            for (CityModule m:list) {
                m.setModuleUrl(OssConfig.IMAGE_URL+m.getModuleUrl());
                imageList.add(m);
            }
        }
        ExcelUtil<CityModule> util = new ExcelUtil<CityModule>(CityModule.class);
        util.exportExcel(response, imageList, "模块数据");
    }

    /**
     * 获取模块详细信息
     */
    @PreAuthorize("@ss.hasPermi('city:module:query')")
    @GetMapping(value = "/{moduleId}")
    public AjaxResult getInfo(@PathVariable("moduleId") Long moduleId)
    {
        return success(cityModuleService.selectCityModuleByModuleId(moduleId));
    }

    /**
     * 新增模块
     */
    @PreAuthorize("@ss.hasPermi('city:module:add')")
    @Log(title = "模块", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CityModule cityModule)
    {
        cityModule.setModuleId(IdUtil.getSnowflakeNextId());
        return toAjax(cityModuleService.insertCityModule(cityModule));
    }

    /**
     * 修改模块
     */
    @PreAuthorize("@ss.hasPermi('city:module:edit')")
    @Log(title = "模块", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CityModule cityModule)
    {
        return toAjax(cityModuleService.updateCityModule(cityModule));
    }

    /**
     * 删除模块
     */
    @PreAuthorize("@ss.hasPermi('city:module:remove')")
    @Log(title = "模块", businessType = BusinessType.DELETE)
	@DeleteMapping("/{moduleIds}")
    public AjaxResult remove(@PathVariable Long[] moduleIds)
    {
        return toAjax(cityModuleService.deleteCityModuleByModuleIds(moduleIds));
    }
}
